//
//  HLMessageSessionSettingViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/6/14.
//

#import "HLMessageSessionSettingViewController.h"
#import "HLUserDetailViewController.h"
#import "HLReportViewController.h"
#import "HLMineCell.h"
#import "HLMineCellModel.h"

@interface HLMessageSessionSettingViewController ()

@property (nonatomic, strong) HLUserInfoModel *user;
@property (nonatomic, strong) NIMSession *session;

@end

@implementation HLMessageSessionSettingViewController

- (instancetype)initWithUser:(HLUserInfoModel *)user session:(NIMSession *)session
{
    if (self = [super init]){
        self.user = user;
        self.session = session;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HLBackgroundColor;
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    self.title = @"好友设置";
}

- (void)initTableView
{
    [super initTableView];
    
    [self.datasources addObjectsFromArray:[HLMineCellModel cellWithSessionSettingWithUser:_user session:_session]];
    
    self.tableView.backgroundColor = HLBackgroundColor;
    [self.tableView registerClass:[HLMineCell class] forCellReuseIdentifier:[HLMineCell identifyCell]];
    
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datasources.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sections = self.datasources[section];
    return sections.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLMineCell identifyCell]];
    cell.model = self.datasources[indexPath.section][indexPath.row];
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = HLBackgroundColor;
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 12;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLMineCellModel *model = self.datasources[indexPath.section][indexPath.row];
    
    switch (model.cellSpot) {
        case HLMineCellSpotSessionInfo:
        {
            HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:_user.userId];
            [self.navigationController pushViewController:userDetailVC animated:YES];
        }break;
        case HLMineCellSpotSessionReport:
        {
            HLReportViewController *reportVC = [[HLReportViewController alloc] initWithType:HLReportTypeMine userId:_user.userId];
            [self.navigationController pushViewController:reportVC animated:YES];
        }break;
        case HLMineCellSpotSessionDelete:
        {
            MJWeakSelf
            [HLUIHelper showAlertWithTitle:@"温馨提示" content:@"确定清空所有本地聊天记录？" callBack:^(BOOL isConfirm) {
                if (isConfirm){
                    [NIMSDK.sharedSDK.conversationManager deleteAllmessagesInSession:weakSelf.session option:nil];
                    NIMSessionDeleteAllRemoteMessagesOptions *opt = [[NIMSessionDeleteAllRemoteMessagesOptions alloc] init];
                    [NIMSDK.sharedSDK.conversationManager deleteAllRemoteMessagesInSession:weakSelf.session options:opt completion:^(NSError * _Nullable error) {
                        NSLog(@"del session %@", error.localizedDescription);
                    }];
                    [QMUITips showWithText:@"清空成功！"];
                }
            }];
        }break;
        case HLMineCellSpotSessionBlack:
        {
            MJWeakSelf;
            [HLUIHelper blackWithUserId:_user.userId black:[_user.isBlacklist boolValue] callback:^(BOOL isConfirm) {
                if (isConfirm){
                    weakSelf.user.isBlacklist = [weakSelf.user.isBlacklist boolValue] ? @"0":@"1";
                    [weakSelf reloadSetting];
                }
            }];
        }break;
        case HLMineCellSpotSessionFollow:
        {
            BOOL hasFollow = [_user.followType isEqualToString:@"0"];
            MJWeakSelf;
            [HLUIHelper followWithUserId:_user.userId follow:hasFollow callback:^(BOOL isConfirm) {
                if (isConfirm){
                    weakSelf.user.followType = hasFollow ? @"1":@"0";
                    [weakSelf reloadSetting];
                }
            }];
        }break;
        case HLMineCellSpotSessionTop:
        {
            NIMStickTopSessionInfo *info = [NIMSDK.sharedSDK.chatExtendManager stickTopInfoForSession:self.session];
            MJWeakSelf
            if (info){
                [NIMSDK.sharedSDK.chatExtendManager removeStickTopSession:info completion:^(NSError * _Nullable error, NIMStickTopSessionInfo * _Nullable removedInfo) {
                    if (error) {
                        [QMUITips showWithText:error.localizedDescription];
                    }
                    [NSNotificationCenter.defaultCenter postNotificationName:HLUpdateStickTopNotification object:@"1" userInfo:@{@"key":removedInfo, @"type":@"0"}];
                    [weakSelf reloadSetting];
                }];
            } else {
                NIMAddStickTopSessionParams *params = [[NIMAddStickTopSessionParams alloc] initWithSession:self.session];
                [NIMSDK.sharedSDK.chatExtendManager addStickTopSession:params completion:^(NSError *         _Nullable error, NIMStickTopSessionInfo * _Nullable newInfo) {
                    if (error) {
                        [QMUITips showWithText:error.localizedDescription];
                    }
                    [NSNotificationCenter.defaultCenter postNotificationName:HLUpdateStickTopNotification object:@"1" userInfo:@{@"key":newInfo, @"type":@"1"}];
                    [weakSelf reloadSetting];
                }];
            }
        }break;
            
        default:
            break;
    }
    

    
}

- (void)reloadSetting
{
    [self.datasources removeAllObjects];
    [self.datasources addObjectsFromArray:[HLMineCellModel cellWithSessionSettingWithUser:_user session:_session]];
    [self.tableView reloadData];
}

@end
